<!--
- Author:   .
- Date:    2017/8/2 0002.
- File:    index.
-->
<template>
  <div class="">
    <div class="logo2" style="text-align: center; font-size: 26px; color: #E67A7E; width: 100%;">云南云香会员平台</div>
    <div class="login_cont">
      <div class="login_ipt">
        <input type="tel" placeholder="您的ID号" v-model="mobile">
      </div>
      <div class="login_ipt">
        <input type="password" placeholder="密码" v-model="pwd"><label>
      </label>
      </div>
      <div class="login_btn">
        <button @click="login" style="background-color: #E67A7E;">登录</button>
      </div>
      <div class="login_other" style=" padding-top:10px;margin-top: 20px;">
        <ul class="clear">
          <li style=" float:left; text-align:left;">
            <a href="javascript:void(0)" @click="gotoModifyPwd"><span>忘记密码</span></a>
          </li>
          <li style=" float:right; text-align:right;">
            <a href="javascript:void(0)" @click="gotoLoginMessage"><span>快速注册</span></a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import messageCode from '../../components/message-code.vue'

  export default {
    beforeRouteEnter(to, from, next) {
      if (!document.body.classList.contains('login_bg')) {
        document.body.classList.add('login_bg')
      }
      next()
    },
    beforeRouteLeave(to, from, next) {
      if (to.name != 'login-password' && to.name != 'login-bind') {
        document.body.classList.remove('login_bg')
      }
      next()
    },
    components: {
      'message-code': messageCode
    },
    data() {
      return {
        fkmid: this.$storage.getFkmid() || null,
        mobile: '',
        pwd: ''
      }
    },
    created() {
      console.log(this.isWeiXin())
    },
    methods: {
      gotoModifyPwd() {
        this.$router.push({name: 'setting-password'})
      },
      gotoLoginMessage() {
        this.$router.replace({name: 'login-message'})
      },
      login() {
        if (this.mobile == '') {
          this.$toast('请输入唯一ID号')
          return
        }
        if (this.pwd == '') {
          this.$toast('请输入密码')
          return
        }
        this.$request.loginByPwd(this.mobile, this.pwd).then(data => {
          this.$storage.setKey(data.key)
//          if (!data.member_truename) {
//            this.$toast('请先填写真实姓名')
//            this.$router.replace({name: 'setting-truename', query: {fkmid: this.fkmid}})
//          } else {
          if (this.mobile == '18350035286') { // 提供给h5支付申请的测试账号，跳转到支付页 方便审核
            this.$router.replace({name: 'member-tobeVip'})
          } else {
            this.$router.replace({name: 'my', query: {fkmid: this.fkmid}})
          }
//          }
        }).catch(e => {
          this.$toast('登录失败:' + e.msg)
        })
      },
      wxLogin() {
        this.$native.weixinLogin().then(wxInfo => {
          console.log('微信登录成功', wxInfo)
          this.wxInfo = wxInfo
          this.$request.getKeyByUnionId(wxInfo.unionid).then(data => {
            console.log('根据unionid获取用户信息', data)
            if (data.key) {
              this.$storage.setKey(data.key)
              this.$router.replace({name: 'my', query: {fkmid: this.fkmid}})
            } else {
              this.$router.replace({name: 'login-bind'})
            }
          })
        }).catch(e => {
          this.$toast('微信登录失败:' + e.msg)
        })
      },
      isWeiXin() {
        let ua = window.navigator.userAgent.toLowerCase()
        console.log(ua)
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
          return 'yes'
        } else {
          return 'no'
        }
      }
    }
  }
</script>

<style>

</style>
